﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using QLHD.BLL;
using QLHD.DAL;

namespace QLHD
{
    public partial class frmChiTietTH_TN : Form
    {
        string maauto;
        public frmChiTietTH_TN() { }
        public frmChiTietTH_TN(frmMain form)
        {
            InitializeComponent();
            MdiParent = form;

        }

        private void frmChiTietTH_TN_Load(object sender, EventArgs e)
        {
            LoadCombobox();
            LoadComboboxManhom();
            dgvTHTN.DataSource = TH_TN_DB.SelectTHTN();
            DisplayData(0);
            ReadOnlyTextbox();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        void LoadCombobox()
        {
            cbbMaLo.DataSource = Lo_DB.SelectLo();
            cbbMaLo.DisplayMember = "diengiai";
            cbbMaLo.ValueMember = "malo";

            cbbMaTP.DataSource = MaTP_DB.SelectMaTP_THTN();
            cbbMaTP.DisplayMember = "matp";
            cbbMaTP.ValueMember = "matp";

            cbbMaDV.DataSource = DonVi_DB.SelectDV();
            cbbMaDV.DisplayMember = "tendv";
            cbbMaDV.ValueMember = "madv";
        }
        void LoadComboboxManhom()
        {
            cbbMaNhom.DataSource = Nhom_DB.spSelectAllNhomByMalo(cbbMaLo.SelectedValue.ToString());
            cbbMaNhom.DisplayMember = "diengiai";
            cbbMaNhom.ValueMember = "manhom";
        }

        private void cbbMaLo_SelectionChangeCommitted(object sender, EventArgs e)
        {
            LoadComboboxManhom();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
                       
            if (btnThem.Text == "Tạo mới")
            {
                btnThem.Text = "Lưu";
                dtpNgayGiao.Text = DateTime.Now.ToString();
                dtpNgayGiao.Enabled = false;
                AutoMaXuatNLTho();
                txtMaCT_TN.Text = maauto;
                cbbMaDV.Text = "--Chọn đơn vị--";
                cbbMaLo.Text = "--Chọn mã lô--";
                cbbMaNhom.Text = "--Chọn mã nhóm";
                cbbMaTP.Text = "--Chọn mã loại"; 
                NotReadOnlyTextbox();
                ClearTextbox();

            }
            else
            {
                try
                {
                    InsertTN_TN();
                    frmChiTietTH_TN_Load(sender, e);
                }
                catch (SqlException ex)
                {
                    MessageBox.Show("Xảy ra lỗi" + "\n" + ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtDinhmuc.Focus();
                }
            }         
        }

        private void InsertTN_TN()
        {          
            TH_TN tachnhan = new TH_TN();
            tachnhan.MaCT_TN = txtMaCT_TN.Text;
            tachnhan.Malo = cbbMaLo.SelectedValue.ToString();
            tachnhan.Manhom = cbbMaNhom.SelectedValue.ToString() ;
            tachnhan.MaTP = cbbMaTP.SelectedValue.ToString();
            tachnhan.MaDV = cbbMaDV.SelectedValue.ToString();
            try
            {
                tachnhan.KL_hangvo = float.Parse(txtKL_hangvo.Text);
                tachnhan.Kl_nhannguyen = float.Parse(txtKL_nhannguyen.Text);
                tachnhan.Kl_nhanbe = float.Parse(txtKL_Nhanbe.Text);
                tachnhan.Dinhmuc = float.Parse(txtDinhmuc.Text);
            }
            catch (Exception)
            {
                tachnhan.KL_hangvo = 0;
                tachnhan.Kl_nhannguyen = 0;
                tachnhan.Kl_nhanbe = 0;
                tachnhan.Dinhmuc = 0;
            }          
            tachnhan.Ngaygiao = dtpNgayGiao.Text;
            tachnhan.Ngaythuhoi = dtpNgayTH.Text;
            TH_TN_DB.InsertTHTN(tachnhan);
            btnThem.Text = "Tạo mới";
            ReadOnlyTextbox();
        }

        private void ReadOnlyTextbox()
        {
            txtDinhmuc.ReadOnly = true;
            txtKL_hangvo.ReadOnly = true;
            txtKL_nhannguyen.ReadOnly = true;
            txtKL_Nhanbe.ReadOnly = true; 
        }
        private void DisplayData(int index)
        {
            
            double hangvo, nhannguyen, nhanbe, tc, tcvo ;
            try
            {
                txtMaCT_TN.Text = dgvTHTN.Rows[index].Cells[0].Value.ToString();
                cbbMaLo.Text = dgvTHTN.Rows[index].Cells[1].Value.ToString();
                cbbMaNhom.Text = dgvTHTN.Rows[index].Cells[2].Value.ToString();
                cbbMaTP.Text = dgvTHTN.Rows[index].Cells[3].Value.ToString();
                cbbMaDV.Text = dgvTHTN.Rows[index].Cells[4].Value.ToString();
                txtKL_hangvo.Text = dgvTHTN.Rows[index].Cells[5].Value.ToString();
                txtKL_nhannguyen.Text = dgvTHTN.Rows[index].Cells[6].Value.ToString();
                txtKL_Nhanbe.Text = dgvTHTN.Rows[index].Cells[7].Value.ToString();
                txtNguyenVo.Text =Math.Round(double.Parse(dgvTHTN.Rows[index].Cells[8].Value.ToString()),3).ToString();
                txtNguyenNhan.Text =Math.Round(double.Parse(dgvTHTN.Rows[index].Cells[9].Value.ToString()),3).ToString();
                txtBeVo.Text = Math.Round(double.Parse(dgvTHTN.Rows[index].Cells[10].Value.ToString()),3).ToString();
                txtBeNhan.Text = Math.Round(double.Parse(dgvTHTN.Rows[index].Cells[11].Value.ToString()), 3).ToString();
                txtDinhmuc.Text = dgvTHTN.Rows[index].Cells[12].Value.ToString();
                dtpNgayGiao.Text = dgvTHTN.Rows[index].Cells[13].Value.ToString();

                hangvo = double.Parse(txtKL_hangvo.Text);
                nhannguyen = double.Parse(txtKL_nhannguyen.Text);
                nhanbe = double.Parse(txtKL_Nhanbe.Text);

                tc = Math.Round(nhannguyen + nhanbe, 2);

                tcvo = Math.Round((tc / hangvo) * 100, 3);

                txtTC.Text = tc.ToString();

                txtTCVo.Text = tcvo.ToString();

                //txtBeTC.Text = betc.ToString(); Xem lại chỗ này
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        private void ClearTextbox()
        {
            txtDinhmuc.Clear();
            txtKL_hangvo.Clear();
            txtKL_nhannguyen.Clear();
            txtKL_Nhanbe.Clear();
            txtTC.Clear();
            txtBeNhan.Clear();
            
            txtBeVo.Clear();
            txtNguyenNhan.Clear();
            txtNguyenVo.Clear();
            txtTCVo.Clear();
            
        }

        private void NotReadOnlyTextbox()
        {
            txtDinhmuc.ReadOnly = false;
            txtKL_hangvo.ReadOnly = false;
            txtKL_nhannguyen.ReadOnly = false;
            txtKL_Nhanbe.ReadOnly = false;     
           
        }

        private void AutoMaXuatNLTho()
        {
            maauto = "";
            SqlConnection con = Connection.GetConnection();
            con.Open();
            string select = "select top 1 MaCT_TN from tblChiTiet_TN where NgayGiao = '" + dtpNgayGiao.Text + "' order by MaCT_TN desc";
            DataTable dt = new DataTable();
            SqlCommand command = new SqlCommand(select, con);
            SqlDataAdapter da = new SqlDataAdapter(command);
            da.Fill(dt);

            if (dt.Rows.Count == 0)
            {
                maauto = dtpNgayGiao.Text.Replace("/", "") + "1";
            }
            else
            {
                maauto = (Convert.ToInt32(dt.Rows[0][0]) + 1).ToString();
            }
        }

        private void dgvTHTN_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DisplayData(e.RowIndex);
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            frmChiTietTH_TN_Load(sender, e);
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            try
            {
                if (btnSua.Text == "Cập nhật")
                {
                    btnSua.Text = "Lưu";
                    NotReadOnlyTextbox();
                
                }
                else
                {
                    TH_TN tachnhan = new TH_TN();
                    tachnhan.MaCT_TN = txtMaCT_TN.Text;
                    tachnhan.Malo = cbbMaLo.SelectedValue.ToString();
                    tachnhan.Manhom = cbbMaNhom.SelectedValue.ToString();
                    tachnhan.MaTP = cbbMaTP.SelectedValue.ToString();
                    tachnhan.MaDV = cbbMaDV.SelectedValue.ToString();
                    try
                    {
                        tachnhan.KL_hangvo = float.Parse(txtKL_hangvo.Text);
                        tachnhan.Kl_nhannguyen = float.Parse(txtKL_nhannguyen.Text);
                        tachnhan.Kl_nhanbe = float.Parse(txtKL_Nhanbe.Text);
                        tachnhan.Dinhmuc = float.Parse(txtDinhmuc.Text);
                    }
                    catch (Exception)
                    {
                        tachnhan.KL_hangvo = 0;
                        tachnhan.Kl_nhannguyen = 0;
                        tachnhan.Kl_nhanbe = 0;
                        tachnhan.Dinhmuc = 0;
                    }
                    tachnhan.Ngaygiao = dtpNgayGiao.Text;
                    tachnhan.Ngaythuhoi = dtpNgayTH.Text;

                    TH_TN_DB.UpdateTHTN(txtMaCT_TN.Text, tachnhan);
                    btnSua.Text = "Cập nhật";
                    frmChiTietTH_TN_Load(sender,e);
                    ReadOnlyTextbox();
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show("Xảy ra lỗi" + "\n" + ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtKL_hangvo.Focus();
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                TH_TN_DB.DeleteTHTN(txtMaCT_TN.Text);
                frmChiTietTH_TN_Load(sender, e);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
